package cn.tedu.egweibo2.controller;

import cn.tedu.egweibo2.base.utils.CacheUtil;
import cn.tedu.egweibo2.base.response.JsonResult;
import cn.tedu.egweibo2.base.response.Status;
import cn.tedu.egweibo2.mapper.WeiboMapper;
import cn.tedu.egweibo2.pojo.dto.WeiboAddParam;
import cn.tedu.egweibo2.pojo.entity.Weibo;
import cn.tedu.egweibo2.pojo.vo.UserVO;
import cn.tedu.egweibo2.pojo.vo.WeiboDetailVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
@Validated
@Slf4j
@Api(tags="02.微博管理模块")
@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {
    @Autowired
    WeiboMapper weiboMapper;
    @Autowired
    CacheUtil cacheUtil;
    @PostMapping("insert")
    @ApiOperation(value = "发布微博")
    @ApiOperationSupport(order = 10)
    public JsonResult insertWeibo(@RequestBody WeiboAddParam weiboAddParam,@ApiIgnore HttpSession session){
        log.debug("weiboAddParam="+weiboAddParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO == null){
            return new JsonResult(Status.NOT_LOGIN);
        }
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboAddParam,weibo);
        weibo.setUserId(userVO.getId());
        weibo.setCreateTime(new Date());
        weiboMapper.insertWeibo(weibo);
        return JsonResult.ok();
    }
    @GetMapping("selectIndex")
    @ApiOperation(value = "微博首页列表")
    @ApiOperationSupport(order = 20)
    public JsonResult selectIndex(@ApiIgnore HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO == null){
            return new JsonResult(Status.NOT_LOGIN);
        }
        Serializable weiboIndex = cacheUtil.get("weibo_Index");
        if(weiboIndex !=null){
            log.debug("首页数据来自redis数据库...");
           return JsonResult.ok(weiboIndex);
        }
        List<Weibo> weiboIndexVOS =weiboMapper.selectIndex();
        log.debug("首页数据来自Mysql数据库...");
        cacheUtil.setExpire("weibo_index", (Serializable)weiboIndexVOS,30);
        return JsonResult.ok(weiboIndexVOS);
    }
    @GetMapping("selectById")
    @ApiOperation(value = "微博详情页")
    @ApiOperationSupport(order = 30)
    public JsonResult selectById(@Range(min=200,max=500,message = "微博id必须在200-500之间") Long id, @ApiIgnore HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO == null){
            return new JsonResult(Status.NOT_LOGIN);
        }
        List<WeiboDetailVO> list = weiboMapper.selectById(id);
        return JsonResult.ok(list);
    }
}
